/*
two functionalities in DPGmanager:
1. deployer
2. forking a result receiver which is the sink node in the DPG
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "config.h"
#include "ffs.h"
#include "cod.h"
#include "evpath.h"
#include "ev_dfg.h"
#include "util.h"
#include <math.h>

static EVdfg test_dfg;

char *COD_generate = "{\n\
    output.integer_field = generate_record();\n\
}";
/*
static char *trans = "{\n\
    simple* a;\n\
    int i;\n\
    int size;\n\
    size = EVcount_simple();\n\
    printf(\"queue size \%d\\n\", size);\n\ 
    for(i = 0; i < size; i++) {\n\
      a = EVdata_simple(i);\n\
      printf(\"data: \%d\\n\", a.integer_field);\n\
    }\n\
    if(size == 5) {\n\
      for(i = 0; i < size; i++) {\n\
        printf(\"discard: \%d\\n\", i);\n\
        EVdiscard_simple(0);\n\
      }\n\ 
    }\n\
    return 0;\n\
}\0\0";

static FMStructDescList queue_list[] = {simple_format_list, NULL};

int main(int argc, char*argv[]) {
    
    char **ids = new char*[4];  
    ids[0] = "master";
    ids[1] = "client1";
    //ids[2] = "client2";
    ids[2] = NULL;

    CManager cm;
    attr_list contact_list;
    char *str_contact;
    EVdfg_stone* nodes = new EVdfg_stone[4];

    cm = CManager_create();
    CMlisten(cm);
    contact_list = CMget_contact_list(cm);
    str_contact = attr_list_to_string(contact_list);
    printf("%s\n", str_contact);
    
    test_dfg = EVdfg_create(cm);
    EVdfg_register_node_list(test_dfg, &ids[0]);
    
    //EVdfg_register_sink_handler(cm, "simple_handler", simple_format_list,
    //                            (EVSimpleHandlerFunc) simple_handler);
    //nodes[0] = EVdfg_create_sink_stone(test_dfg, "simple_handler");
    char* filter = create_multityped_action_spec(queue_list, trans);
    nodes[0] = EVdfg_create_stone(test_dfg, filter);
    EVdfg_assign_node(nodes[0], ids[0]);
    char *action_spec = create_transform_action_spec(NULL,simple_format_list,COD_generate);
    
    //nodes[2] = EVdfg_create_stone(test_dfg, NULL);
    //EVdfg_assign_node(nodes[2], ids[2]);
    //EVdfg_enable_auto_stone(nodes[2], 1, 0);
    //EVdfg_link_port(nodes[2], 0, nodes[0]);

    nodes[1] = EVdfg_create_stone(test_dfg, action_spec);
    EVdfg_assign_node(nodes[1], ids[1]);
    EVdfg_enable_auto_stone(nodes[1], 1, 0);
    EVdfg_link_port(nodes[1], 0, nodes[0]); 
    
  
    EVdfg_realize(test_dfg);
    EVdfg_join_dfg(test_dfg, "master", str_contact);
    printf("begin ready wait! \n");
    EVdfg_ready_wait(test_dfg);
    printf("return from ready wait!\n");
    CMrun_network(cm);
}
